package com.wms.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms.common.QueryPageParam;
import com.wms.common.Result;
import com.wms.entity.Goods;
import com.wms.entity.Menu;
import com.wms.entity.User;
import com.wms.service.GoodsService;
import com.wms.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wms
 * @since 2023-02-05
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;



    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Goods goods){
        return goodsService.save(goods)?Result.suc():Result.fail();
    }

    /*根据id删除*/
    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return goodsService.removeById(id)?Result.suc():Result.fail();
    }

    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody Goods goods){
        return goodsService.updateById(goods);
    }

    /*更新*/
    @PostMapping("/update")
    public Result update(@RequestBody Goods goods){
        return goodsService.updateById(goods)?Result.suc():Result.fail();
    }

    //新增或修改
    @PostMapping("/saveorMod")
    public boolean saveorMod(@RequestBody Goods goods){
        return goodsService.saveOrUpdate(goods);
    }

    //删除
    @GetMapping("/delete")
    public  boolean delete(Integer id){
        return goodsService.removeById(id);
    }

    @PostMapping("/listPage1")
    public  Result listPage1(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        String storage = (String)param.get("storage");
        String goodstype = (String)param.get("goodstype");
        String roleId = (String)param.get("roleId");
        System.out.println("name==="+(String)param.get("name"));



        Page<Goods> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        LambdaQueryWrapper<Goods> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(name)&&!"null".equals(name)){
            lambdaQueryWrapper.like(Goods::getName,name);

        }
        if(StringUtils.isNotBlank(storage)&&!"null".equals(storage)){
            lambdaQueryWrapper.eq(Goods::getStorage,storage);

        }
        if(StringUtils.isNotBlank(goodstype)&&!"null".equals(goodstype)){
            lambdaQueryWrapper.eq(Goods::getGoodstype,goodstype);

        }


        IPage result = goodsService.page(page,lambdaQueryWrapper);


        return Result.suc(result.getRecords(), result.getTotal());
    }


}
